import datetime

from flask import Blueprint, request, jsonify

from model.job import Job
from model.user_collection import UserCollection
from utils.securtiy import login_required, get_user_id
from utils.work_db import get_session
from utils.work_log import get_logger
from dao.job_dao import JobDao

log = get_logger(__name__)

api = Blueprint('collection', __name__, url_prefix='/api/v1/collection')


@api.route('/list', methods=['POST'])
@login_required
def collect_list(**kwargs):
    """
    收藏列表
    :param kwargs:
    :return:
    """
    user_id = kwargs.get("user_id")
    session = get_session()
    p_dict = JobDao.get_province_dict(session)
    q = session.query(UserCollection, Job) \
        .join(Job, UserCollection.job_id == Job.job_id)
    q = q.filter(UserCollection.user_id == user_id)
    result = []
    for obj in q.all():
        result.append(
            {
                'job_name': obj.Job.job_name,
                'job_place': obj.Job.job_place,
                'job_time': obj.Job.job_time,
                'job_id': obj.Job.job_id,
                'salary_code': obj.Job.salary_code,
                'part_salary': obj.Job.part_salary,
                'job_status': obj.Job.job_status,
                'job_type': obj.Job.job_type,
                'job_class': obj.Job.job_class,
                'job_count': obj.Job.job_count,
                'job_content': obj.Job.job_content,
                'job_requirement': obj.Job.job_requirement,
                'job_remark': obj.Job.job_remark,
                'job_cycle': obj.Job.job_cycle if obj.Job.job_cycle else '',
                'company_id': obj.Job.company_id,
                'province_code': obj.Job.province_code,
                'city_code': obj.Job.city_code,
                'balance_type': obj.Job.balance_type,
                'country_"code': obj.Job.country_code,
                'edu_code': obj.Job.edu_code,
                "exp_req": obj.Job.exp_req,
                'province_name': p_dict.get(obj.Job.province_code, ''),
                'city_name': p_dict.get(obj.Job.city_code, ''),
                'country_name': p_dict.get(obj.Job.country_code, '')
            }
        )
    return jsonify({'data': result, 'msg': '', 'code': 1})


@api.route('/remove', methods=['POST'])
@login_required
def remove(**kwargs):
    """
    移除用户收藏职位
    :return:
    """
    args = request.json
    user_id = get_user_id()
    session = get_session()
    job_id = args.get('job_id')
    q = session.query(UserCollection)
    q = q.filter(UserCollection.user_id == user_id)
    q = q.filter(UserCollection.job_id == job_id)
    result = q.first()
    if result is None:
        return jsonify({'msg': '该职位未收藏!', 'code': 1})
    session.delete(result)
    session.commit()
    return jsonify({'msg': '成功取消收藏', 'code': 1})


@api.route('/add', methods=['POST'])
@login_required
def add(**kwargs):
    """
    用户收藏职位
    :return:
    """
    args = request.json
    user_id = get_user_id()
    session = get_session()
    q = session.query(UserCollection)
    q = q.filter(UserCollection.job_id == args.get("job_id"))
    q = q.filter(UserCollection.user_id == user_id)
    result = q.first()
    if result is None:
        user_collection = UserCollection()
        user_collection.user_id = kwargs.get('user_id')
        user_collection.job_id = args.get('job_id')
        user_collection.collect_date = datetime.datetime.now()
        session.add(user_collection)
        session.commit()
        return jsonify({'msg': '收藏成功!', 'code': 1})
    else:
        return jsonify({'msg': '职位已收藏!', 'code': 1})
